// Generated file

// clang-format off
const char *g_detail_modifier_shader_template_0 = 
"#version 450\n"
"\n"
"// Takes a list of positions and evaluates a signed distance field in 4 locations around them.\n"
"// The 4 locations are picked such that the result can be used to compute a gradient.\n"
"// The result is then applied on top of previous values using a specified operation.\n"
"\n"
"// This shader may be dispatched multiple times for each source of voxel data that we may combine for a given chunk.\n"
"\n"
"layout (local_size_x = 4, local_size_y = 4, local_size_z = 4) in;\n"
"\n"
"layout (set = 0, binding = 0, std430) restrict readonly buffer PositionBuffer {\n"
"	// X, Y, Z is hit position\n"
"	// W is integer triangle index\n"
"	vec4 values[];\n"
"} u_positions;\n"
"\n"
"layout (set = 0, binding = 1, std430) restrict readonly buffer DetailParams {\n"
"	int tile_size_pixels;\n"
"	float pixel_world_step;\n"
"} u_detail_params;\n"
"\n"
"layout (set = 0, binding = 2, std430) restrict readonly buffer InSDBuffer {\n"
"	// 4 values per index\n"
"	float values[];\n"
"} u_in_sd;\n"
"\n"
"layout (set = 0, binding = 3, std430) restrict writeonly buffer OutSDBuffer {\n"
"	// 4 values per index\n"
"	float values[];\n"
"} u_out_sd;\n"
"\n"
"// Parameters common to all modifiers\n"
"layout (set = 0, binding = 4, std430) restrict readonly buffer BaseModifierParams {\n"
"	mat4 world_to_model;\n"
"	int operation;\n"
"	float smoothness;\n"
"	float sd_scale;\n"
"} u_base_modifier_params;\n"
"\n";
// clang-format on

// clang-format off
const char *g_detail_modifier_shader_template_1 = 
"\n"
"float sd_smooth_union(float a, float b, float s) {\n"
"	const float h = clamp(0.5 + 0.5 * (b - a) / s, 0.0, 1.0);\n"
"	return mix(b, a, h) - s * h * (1.0 - h);\n"
"}\n"
"\n"
"// Inverted a and b because it subtracts SDF a from SDF b\n"
"float sd_smooth_subtract(float b, float a, float s) {\n"
"	const float h = clamp(0.5 - 0.5 * (b + a) / s, 0.0, 1.0);\n"
"	return mix(b, -a, h) + s * h * (1.0 - h);\n"
"}\n"
"\n"
"void main() {\n"
"	const ivec2 pixel_pos_in_tile = ivec2(gl_GlobalInvocationID.xy);\n"
"	const int tile_index = int(gl_GlobalInvocationID.z);\n"
"\n"
"	const int index = pixel_pos_in_tile.x + pixel_pos_in_tile.y * u_detail_params.tile_size_pixels \n"
"		+ tile_index * u_detail_params.tile_size_pixels * u_detail_params.tile_size_pixels;\n"
"\n"
"	if (u_positions.values[index].w == -1.0) {\n"
"		return;\n"
"	}\n"
"\n"
"	vec3 pos0 = u_positions.values[index].xyz;\n"
"	vec3 pos1 = pos0 + vec3(u_detail_params.pixel_world_step, 0.0, 0.0);\n"
"	vec3 pos2 = pos0 + vec3(0.0, u_detail_params.pixel_world_step, 0.0);\n"
"	vec3 pos3 = pos0 + vec3(0.0, 0.0, u_detail_params.pixel_world_step);\n"
"\n"
"	pos0 = (u_base_modifier_params.world_to_model * vec4(pos0, 1.0)).xyz;\n"
"	pos1 = (u_base_modifier_params.world_to_model * vec4(pos1, 1.0)).xyz;\n"
"	pos2 = (u_base_modifier_params.world_to_model * vec4(pos2, 1.0)).xyz;\n"
"	pos3 = (u_base_modifier_params.world_to_model * vec4(pos3, 1.0)).xyz;\n"
"\n"
"	const int sdi = index * 4;\n"
"\n"
"	float sd0 = u_in_sd.values[sdi];\n"
"	float sd1 = u_in_sd.values[sdi + 1];\n"
"	float sd2 = u_in_sd.values[sdi + 2];\n"
"	float sd3 = u_in_sd.values[sdi + 3];\n"
"\n"
"	const float msd0 = get_sd(pos0) * u_base_modifier_params.sd_scale;\n"
"	const float msd1 = get_sd(pos1) * u_base_modifier_params.sd_scale;\n"
"	const float msd2 = get_sd(pos2) * u_base_modifier_params.sd_scale;\n"
"	const float msd3 = get_sd(pos3) * u_base_modifier_params.sd_scale;\n"
"\n"
"	if (u_base_modifier_params.operation == 0) {\n"
"		sd0 = sd_smooth_union(sd0, msd0, u_base_modifier_params.smoothness);\n"
"		sd1 = sd_smooth_union(sd1, msd1, u_base_modifier_params.smoothness);\n"
"		sd2 = sd_smooth_union(sd2, msd2, u_base_modifier_params.smoothness);\n"
"		sd3 = sd_smooth_union(sd3, msd3, u_base_modifier_params.smoothness);\n"
"\n"
"	} else if (u_base_modifier_params.operation == 1) {\n"
"		sd0 = sd_smooth_subtract(sd0, msd0, u_base_modifier_params.smoothness);\n"
"		sd1 = sd_smooth_subtract(sd1, msd1, u_base_modifier_params.smoothness);\n"
"		sd2 = sd_smooth_subtract(sd2, msd2, u_base_modifier_params.smoothness);\n"
"		sd3 = sd_smooth_subtract(sd3, msd3, u_base_modifier_params.smoothness);\n"
"\n"
"	} else {\n"
"		sd0 = msd0;\n"
"		sd1 = msd1;\n"
"		sd2 = msd2;\n"
"		sd3 = msd3;\n"
"	}\n"
"\n"
"	u_out_sd.values[sdi] = sd0;\n"
"	u_out_sd.values[sdi + 1] = sd1;\n"
"	u_out_sd.values[sdi + 2] = sd2;\n"
"	u_out_sd.values[sdi + 3] = sd3;\n"
"}\n"
"\n";
// clang-format on
